<template>
    <view>
        <!-- 搜索组件 -->
		
		<view class="search_input">
			<view class="iconfont icon-guanbi navigator" @tap="homepagesearch">搜索 讲师 课件</view>
		</view>
	
   
		
        <view class="swiper">
            <swiper autoplay="ture" interval="2000" circular="ture">
                <block v-for="(item, index) in imgUrls" :key="index">
                    <swiper-item>
                        <!-- show-menu-by-longpress="ture" 实现长按图片功能 -->
                        <image :src="item.carouselChartUrl" @longpress="saveImage(index)"></image>
                    </swiper-item>
                </block>
            </swiper>
        </view>
        <view class="post">
            <image class="img" src="https://xxzhang.oss-cn-hangzhou.aliyuncs.com/%E5%85%AC%E5%91%8A.png" />
            <swiper class="swiper2" :easy-function="easy_function" :autoplay="true" interval="3000" duration="500" :circular="true" :vertical="true">
                <block v-for="(notice, index) in notice" :key="index">
                    <swiper-item @tap="getonotice" :id="notice.id">
                        <text class="text">· {{ notice.noticeTitle }}</text>
                    </swiper-item>
                </block>
            </swiper>
            <view>
                <navigator url="/pages/notice/notice">
                    <text class="gengduo">更多</text>
                    <image class="gengduoimg" src="https://xxzhang.oss-cn-hangzhou.aliyuncs.com/%E6%9B%B4%E5%A4%9A.png" />
                </navigator>
            </view>
        </view>

        <view class="case-display">
            <block v-for="(item, index) in navigationList" :key="index">
                <view :class="currentId == item.id ? 'active' : 'default'" @tap="selShopTc" :id="item.id">
                    <image :src="item.navigationUrl" />
                    <text>{{ item.name }}</text>
                </view>
            </block>
        </view>

        <!-- 下方列表 -->
        <view class="appointment">
            <block v-for="(courseware, index) in courseware" :key="index">
                <navigator url="url" :id="courseware.id" @tap="details" class="mod-item">
                    <view class="mod-img">
                        <image :src="courseware.userHeadImg" class="mod-image"></image>
                        <view class="head-text">
                            <text>{{ courseware.title }}</text>
                            <view class="mod-info">
                                <view v-if="courseware.classification == 5" class="type">
                                    <text style="font-size: 28rpx">{{ courseware.classificationName }}</text>
                                </view>
                                <view v-if="courseware.classification == 7" class="type">
                                    <text style="font-size: 28rpx">{{ courseware.classificationName }}</text>
                                </view>
                                <view v-if="courseware.classification == 9" class="type">
                                    <text style="font-size: 28rpx">{{ courseware.classificationName }}</text>
                                </view>
                                <view v-if="courseware.classification == 11" class="type">
                                    <text style="font-size: 28rpx">{{ courseware.classificationName }}</text>
                                </view>
                                <view v-if="courseware.membershipPrivileges == 1" class="moinfont"><text style="color: #e06f6a; font-size: 28rpx;">已获得权限</text></view>
<!-- 							    <view v-if="courseware.whichIssue == 2" class="moinfont"><text style="color: #e06f6a; font-size: 28rpx;">￥10.00</text></view -->
                            </view>
                        </view>
                    </view>
                </navigator>
            </block>
        </view>

      <view v-if="courseware.length == 0">
            <view class="wushuju">
                <image style="width: 100rpx; height: 70rpx" src="https://xxzhang.oss-cn-hangzhou.aliyuncs.com/%E6%97%A0%E6%95%B0%E6%8D%AE%20%281%29.png"></image>
                <text style="color: darkgrey">暂无数据</text>
            </view>
        </view>
		
		<view class="add-wrap" @click="uniShare">
		    <image style="width:45rpx;height:45rpx;" src="/static/iconfont/fenxiang.png"></image>
		    <text style="color: white;font-size:smaller;">分享</text>
		</view>

        <view class="btn" v-if="showToTop" @tap="gotop">
			<image style="width:45rpx;height:45rpx;" src="/static/iconfont/backToTop.png"></image>
            <text style="color: white;font-size:smaller;">顶部</text>
        </view>
    </view>
</template>

<script>
// index.js
const app = getApp();
var urlPrefix = app.globalData.urlPrefix;
import UniShare from 'uni_modules/uni-share/js_sdk/uni-share.js';
const uniShare = new UniShare();
import checkVersion from '@/pages/lq-upgrade/checkVersion.js'
export default {
    data() {
        return {
            courseware: [],
            notice: [],
            imgUrls: [],
            navigationList: [],
            currentId: 5,
            userInfo: {},
            avatarUrl: null,
            gender: 0,
            nickName: null,
            showToTop: false,
            easy_function: '',
			appid:'',
			
			userId:null,
			
			token:null
        };
    },
	
	 onBackPress({from}) {
	            console.log(from);
	            if(from=='backbutton'){
	                this.$nextTick(function(){
	                    uniShare.hide()
	                })
	                return uniShare.isShow;
	            }
	        },
	
    onPageScroll(e) {
        if (e.scrollTop > 780) {
            this.setData({
                showToTop: true
            });
        } else {
            this.setData({
                showToTop: false
            });
        }
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
	
    onPullDownRefresh() {
      
        this.indexList();

			let that = this;
			//公告
			uni.request({
				url: urlPrefix + '/api/applet/index/noticeList',
				method: 'GET',
				header: {
					'content-type': 'application/json',
					"token": this.token
					
				},
				success: function (res) {
					let dataList = res.data;
					that.setData({
						notice: dataList.datas
					});
				}
			});
			//轮播图
			uni.request({
				url: urlPrefix + '/api/applet/index/carouselList',
				data: {},
				method: 'GET',
				header: {
					'content-type': 'application/json',
					"token": this.token
				},
				success: function (res) {
					let dataList = res.data;
					that.setData({
						imgUrls: dataList.datas
					});
				}
			});
			
			
			//课件
			uni.request({
				url: urlPrefix + '/api/applet/index/coursewareListIndex?type='+this.currentId,
				method: 'GET',
				header: {
					'content-type': 'application/json',
					"token": this.token
				},
				success: function (res) {
					that.setData({
						courseware: res.data.datas,
					});
				}
			});
						
						
						
			uni.stopPullDownRefresh();  //停止刷新
			uni.showToast({
				title: '刷新成功',
				icon: 'none',
				duration: 2000
			});
		      

    },
    onShareAppMessage() {},
    onShareTimeline(res) {
        // 分享到朋友圈
        return {};
    },
    onShow() {
		var userId = uni.getStorageSync('userId');
		this.userId = userId;
		this.upgrade();
		this.onShowLook();
    },
    onLoad: function (options) {
		var userId = uni.getStorageSync('userId');
		this.userId = userId;
		let token = uni.getStorageSync("token");
		if (!token) {
			uni.reLaunch({
				url: "/pages/login/login",
				success: () => {}
			})
		}
		this.token = token
		uni.showLoading({
		      title: '加载中',
		});
		this.upgrade();
        this.indexList();
    },
	
	 onReachBottom() {
	 
	  },
	
	
    methods: {
		 
		   saveImage(index){
			     let that = this;
			 uni.showModal({
			 	title:'提示',
				content:"是否要保存图片到本地相册？",
				success:function(res){
					if(res.confirm){
						that.saveImg(that.imgUrls[index].carouselChartUrl)
					}else if(res.cancel){
					}
				
				}
			 })
		},
		
		saveImg : function(srcurl){
			uni.getImageInfo({
				src:this.srcurl,
				success(res) {
					uni.saveImageToPhotosAlbum({
						filePath:res.path,
						success:()=>{
							uni.showToast({
							title:'保存成功',
							icon:'success'
							});
						},
						fail: () => {
							uni.showToast({
								title:'保存失败',
								icon:'error'
							});
						}
					});
				}
			});
		},
		
		
			uniShare() {
				uniShare.show({
					content: { //公共的分享参数配置  类型（type）、链接（herf）、标题（title）、summary（描述）、imageUrl（缩略图）
						type: 0,
						href: 'http://47.119.175.250/',
						title: '精准落码课堂',
						summary: '分享精准落码课堂',
						imageUrl: 'https://xxzhang.oss-cn-hangzhou.aliyuncs.com/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20240428112350.png'
					},
					menus: [{
							"img": "/static/app-plus/sharemenu/wechatfriend.png",
							"text": "微信好友",
							"share": { //当前项的分享参数配置。可覆盖公共的配置如下：分享到微信小程序，配置了type=5
								"provider": "weixin",
								"scene": "WXSceneSession"
							}
						},
						{
							"img": "/static/app-plus/sharemenu/wechatmoments.png",
							"text": "微信朋友圈",
							"share": {
								"provider": "weixin",
								"scene": "WXSceneTimeline"
							}
						},
						// {
						// 	"img": "/static/app-plus/sharemenu/more.png",
						// 	"text": "更多",
						// 	"share": "shareSystem"
						// }
					],
					cancelText: "取消分享",
				}, e => { //callback
					console.log(uniShare.isShow);
					console.log(e);
				})
				},
			
		
        gotop: function (e) {
            if (uni.pageScrollTo) {
                uni.pageScrollTo({
                    scrollTop: 0,
                    //滚到顶部
                    duration: 0.5 //滚的到顶部需要的时间  动画
                });
            } else {
                uni.showModal({
                    title: '提示',
                    content: '无法使用该功能，请升级到最新版本后重试。'
                });
            }
        },

        //搜索跳转
        homepagesearch: function (res) {
            uni.navigateTo({
                url: '/pages/searchfor/searchfor'
            });
        },

        details: function (res) {
            var id = res.currentTarget.id;
            uni.navigateTo({
                url: '/pages/coursewaredetails/coursewaredetails?id=' + id,
            });
        },

        getonotice: function (res) {
            uni.navigateTo({
                url: '/pages/noticetext/noticetext?id=' + res.currentTarget.id
            });
        },
		
		
		selShopTc: function (res) {
			uni.showLoading({
			      title: '加载中',
			});
		     let that = this;
		    uni.request({
		        url: urlPrefix + '/api/applet/index/coursewareListIndex?type=' + res.currentTarget.id,
		        method: 'GET',
		        header: {
		            'content-type': 'application/json',
					"token": this.token
		        },
		        success: function (suss) {
		            let dataList = suss.data;
		            that.setData({
		                currentId: res.currentTarget.id,
		                courseware: dataList.datas
		            });
					uni.hideLoading();
		        }
		    });
		},
		
		

		onShowLook(){
			let that = this;
			this.page += 1;
			uni.request({
			    url: urlPrefix + '/api/applet/index/coursewareListIndex?type=5'+'&userId='+this.userId,
			    method: 'GET',
			    header: {
			        'content-type': 'application/json',
					"token": this.token
			    },
			    success: function (res) {
			        let dataList = res.data;
			        that.setData({
			            courseware: dataList.datas,
			        });
						uni.hideLoading();
			    }
			});
		},
		
		

        indexList: function () {
            let that = this;
            //公告
            uni.request({
                url: urlPrefix + '/api/applet/index/noticeList',
                method: 'GET',
                header: {
                    'content-type': 'application/json',
					"token": this.token
                },
                success: function (res) {
                    let dataList = res.data;
                    that.setData({
                        notice: dataList.datas
                    });
                }
            });
            //轮播图
            uni.request({
                url: urlPrefix + '/api/applet/index/carouselList',
                data: {},
                method: 'GET',
                header: {
                    'content-type': 'application/json',
					"token": this.token
                },
                success: function (res) {
                    let dataList = res.data;
                    that.setData({
                        imgUrls: dataList.datas
                    });
                }
            });

            //导航列表
            uni.request({
                url: urlPrefix + '/api/applet/index/navigationList',
                data: {},
                method: 'GET',
                header: {
                    'content-type': 'application/json',
					"token": this.token
                },
                success: function (res) {
                    let dataList = res.data;
                    that.setData({
                        navigationList: dataList.datas
                    });
                }
            });
            //课件
            uni.request({
                url: urlPrefix + '/api/applet/index/coursewareListIndex?type=5'+"&userId="+this.userId,
                method: 'GET',
                header: {
                    'content-type': 'application/json',
					"token": this.token
                },
                success: function (res) {
                    let dataList = res.data;
                    that.setData({
                        courseware: dataList.datas,
                    });
						uni.hideLoading();
                }
            });
        },
		
        upgrade(){
			
			uni.request({
				url: urlPrefix + '/api/app/version/getVersion',
				method: 'GET',
				header: {
					'content-type': 'application/json',
					"token": this.token
				},
				success: function (res) {
			
				   let name=res.data.datas.name;//新版本名称
				   let code=res.data.datas.versionNum;//新版本号（数字）
				   let content=res.data.datas.vdescribe;//更新内容
				   let url=res.data.datas.updatePath;//app下载链接，（演示请换成真正在下载链接）
				   let isForceUpdate=res.data.datas.isForceUpdate;//是否强制更新
				   let isItProactive = 1;
					
				   checkVersion({
						name,//最新版本名称
						code,//最新版本号
						content,//更新内容
						url,//下载链接
						isForceUpdate,//是否强制升级		
						isItProactive
				   })
				}
			});
							
			
			  
		}
    }
};
</script>
<style lang="scss">
@import './index.scss';
</style>
